package com.group.Filter;

import java.io.IOException;
import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
@WebFilter(
		urlPatterns = { "/*" }, 
		initParams = { 
				@WebInitParam(name = "encoding", value = "UTF-8")
		})
public class SetCharacterEncodingFilter implements Filter {
	private String encoding = null;
	private FilterConfig filterConfig = null;

	public SetCharacterEncodingFilter() {
    }

	public void destroy() {
		this.encoding = null;
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding(encoding);
		long startTime = System.currentTimeMillis();
		
		
		/************************************************************************************/
		chain.doFilter(request, response);
		/************************************************************************************/
		response.setCharacterEncoding(encoding);
		long stopTime = System.currentTimeMillis();
		filterConfig.getServletContext().log(new Date(stopTime) + " 响应经过" + this.getClass().getName() + "过滤器，本次"
				+ "相应过程花费" + (stopTime - startTime) + "毫秒");
	}

	public void init(FilterConfig fConfig) throws ServletException {
		filterConfig = fConfig;
		this.encoding = filterConfig.getInitParameter("encoding");
		filterConfig.getServletContext().log("getInitParameter encoding = " + this.encoding);
	}

}
